home *** CD-ROM | disk | FTP | other *** search
/ 1,000+ Great Games / 1_1000 Games.iso / DOSGAMES / BOGGLE.ZIP / SOURCE.ZIP / BOARD.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-18  |  10.6 KB  |  327 lines

  1. /*****************************************************************************
  2. * Program:  BOARD.CPP
  3. * Purpose:  This module handles the events associated with the game board
  4. *           itself.
  5. *****************************************************************************/
  6. #include  "bogwin.hpp"
  7.  
  8. const int  ID_SQUARE  = 101;
  9. const int  BSWidth    = 40;
  10. const int  BSHeight   = 40;
  11.  
  12. //these represent the possible letters on each die
  13. char *cube_faces[] = { "AAEEGN", "ABBJOO", "ACHOPS", "AFFKPS", "AOOTTW",
  14.    "CIMOTU", "DEILRX", "DELRVY", "DISTTY", "EEGHNW",
  15.    "EEINSU", "EHRTVW", "EIOSST", "ELRTTY", "HLNNRZ",
  16. "IHMNUQ" };
  17.  
  18.  
  19. // The following array defines the adjacency matrix used by
  20. //  the application.  It shows which cubes are valid selections
  21. //  Consider the following diagram of cubes on the board:
  22. //
  23. // 12 13 14 15
  24. //  8  9 10 11
  25. //  4  5  6  7
  26. //  0  1  2  3
  27. //
  28. // The first array element says that from cube 0,  cubes one,  four,
  29. // or cube five are the only valid selections.
  30.  
  31. int nums[][9] = { { 1,  4,  5, -1 },
  32.    { 0,  2,  4,  5,  6, -1 },
  33.    { 1,  3,  5,  6,  7, -1 },
  34.    { 2,  6,  7, -1 },
  35.    { 0,  1,  5,  8,  9, -1 },
  36.    { 0,  1,  2,  4,  6,  8,  9, 10, -1 },
  37.    { 1,  2,  3,  5,  7,  9, 10, 11, -1 },
  38.    { 2,  3,  6, 10, 11, -1 },
  39.    { 4,  5,  9, 12, 13, -1 },
  40.    { 4,  5,  6,  8, 10, 12, 13, 14, -1 },
  41.    { 5,  6,  7,  9, 11, 13, 14, 15, -1 },
  42.    { 6,  7, 10, 14, 15, -1 },
  43.    { 8,  9, 13, -1 },
  44.    { 8,  9, 10, 12, 14, -1 },
  45.    { 9, 10, 11, 13, 15, -1 },
  46.    {10, 11, 14, -1 }
  47. } ;
  48.  
  49.  
  50. /*****************************************************************************
  51. * Function: TBogBoard
  52. * Parms:    Pointers to other objects it needs to talk to
  53. * Purpose:  Instantiate the squares
  54. * Returns:  Nothing
  55. *****************************************************************************/
  56. TBogBoard::TBogBoard (unsigned long id, IWindow* parentAndOwner, 
  57.                       TBogWindow* PBWindow, TBogWIP* PBWIP, 
  58.                       TBogWordList* PBList, TBogScore* PBScore,
  59.                        Sound *pSound)
  60.     : ICanvas(id, parentAndOwner, parentAndOwner)
  61. {
  62.    int row, col, k = 0;
  63.  
  64.    addHandler((ICommandHandler *)this);
  65.    addHandler((IPaintHandler *)this);
  66.    addHandler((IMouseClickHandler *)this);
  67.  
  68.    /**************************************************
  69.    * Instantiate the dictionary
  70.    **************************************************/
  71.    brd_words = new Dictionary("INDEX", "WORDS");
  72.  
  73.    /**************************************************
  74.    * This will keep track of the last selected
  75.    * square so only adjacent squares can be chosen
  76.    * next.
  77.    **************************************************/
  78.    PBLastSquare = new LastSquare();
  79.    pbwip = PBWIP;
  80.    pblist = PBList;
  81.  
  82.    /**************************************************
  83.    * Draw the board using a canvas as a rectangle on
  84.    * canvas client area.
  85.    **************************************************/
  86.    pboard = this;
  87.    pboard->setColor(ICanvas::background, IColor::yellow);
  88.  
  89.    /********************************************************
  90.    * Instantiate the cubes with their appropriate letters
  91.    ********************************************************/
  92.    for (row = 0; row < 4; row++)
  93.    {
  94.       for (col = 0; col < 4; col++)
  95.       {
  96.       try{
  97.          PBSquare[k] = new TBogSquare(k, this,
  98.                                       " ",
  99.                                       PBWindow,
  100.                                       (TBogBoard*)this,
  101.                                       PBLastSquare,
  102.                                       PBWIP,
  103.                                       PBList,
  104.                                       PBScore,
  105.                                       pSound,
  106.                                       row, col, 
  107.                                       IRectangle(IPoint(col*(40+2)+15, row*(40+2)+15),
  108.                                                  ISize(40,40)));
  109.       }
  110.       catch(IException& exc)
  111.       {
  112.          IMessageBox abortIt(IWindow::desktopWindow());
  113.          abortIt.setTitle("Exception Caught in Boggle.cpp");
  114.          abortIt.show(exc.text(), IMessageBox::okButton);
  115.       }
  116.  
  117.          Cubes[k] = new Dice(cube_faces[k]);
  118.          k++;
  119.       }
  120.    }
  121.  
  122. }
  123.  
  124.  
  125. /*****************************************************************************
  126. * Function: ~TBogBoard
  127. * Parms:    none
  128. * Purpose:  destructor
  129. * Returns:  Nothing
  130. *****************************************************************************/
  131. TBogBoard::~TBogBoard ()
  132. {
  133.    int k;
  134.    delete brd_words;
  135.    delete PBLastSquare;
  136.  
  137.    for(k=0; k<16; k++)
  138.       delete Cubes[k];
  139. }
  140.  
  141. /*****************************************************************************
  142. * Function: paintWindow
  143. * Parms:    none
  144. * Purpose:  Process paint messages if you need to
  145. * Returns:  Nothing
  146. *****************************************************************************/
  147. Boolean TBogBoard::paintWindow(IPaintEvent &event)
  148. {
  149.    return false;
  150. }
  151.  
  152.  
  153. /*****************************************************************************
  154. * Function: mouseClicked
  155. * Parms:    evt
  156. * Purpose:  Process mouse clicked messages
  157. * Returns:  Nothing
  158. *****************************************************************************/
  159. Boolean TBogBoard :: mouseClicked(IMouseClickEvent &evt)
  160. {
  161.    return false;
  162. }
  163.  
  164.  
  165. /*****************************************************************************
  166. * Function: command
  167. * Parms:    event
  168. * Purpose:  process the command event
  169. * Returns:  false
  170. *****************************************************************************/
  171. Boolean TBogBoard::command(ICommandEvent& event)
  172. {
  173.    Boolean stopProcessingEvent = false;
  174.  
  175.    switch (event.commandId())
  176.    {
  177.       default:
  178.          break;
  179.    }
  180.    return stopProcessingEvent;
  181. }
  182.  
  183. /*****************************************************************************
  184. * Function: CanvasPointer
  185. * Parms:    none
  186. * Purpose:  return a pointer to the canvas for those who need it
  187. * Returns:  pointer to the board
  188. *****************************************************************************/
  189. ICanvas *TBogBoard::CanvasPointer()
  190. {
  191.    return pboard;
  192. }
  193.  
  194. /*****************************************************************************
  195. * Function: InitBoard
  196. * Parms:    none
  197. * Purpose:  Roll the dice and put some squares on the board
  198. * Returns:  nothing
  199. *****************************************************************************/
  200. void TBogBoard::InitBoard ()
  201. {
  202.    int iIndex, i, j, k=0;
  203.    char *szFace;
  204.    char SquareText[3] = "\0\0";
  205.   
  206.    rLetter.perm();                                         //create random order of cubes
  207.    for (i = 0; i < 4; i++)
  208.    {
  209.       for (j = 0; j < 4; j++)
  210.       {
  211.          iIndex = rLetter.getValue(k);    //determine which cube to use
  212.   
  213.          szFace = Cubes[iIndex]->Roll();
  214.   
  215.          SquareText[0] = *szFace;                  //check for Q!!!
  216.          if (*szFace == 'Q') SquareText[1] = 'u';
  217.   
  218.          /*******************************************
  219.          ** Tell the square what letter to display.
  220.          *******************************************/
  221.          PBSquare[k]->setSquareText(SquareText);
  222.   
  223.          fndall_str[k] = *szFace;
  224.          k++;
  225.          SquareText[1] = '\0';                                  //Reset the Q text!!
  226.       }
  227.    }
  228.    fndall_str[k] = '\0';
  229.    memset(stack, 0, sizeof(stack));    //set these to zero so
  230.    memset(chflag, 0, sizeof(chflag));  // the findall function can keep track
  231. }                                      // of what it has searched thus far
  232.   
  233. /*****************************************************************************
  234. * Function: ClearBoard
  235. * Parms:    none
  236. * Purpose:  set the squares back to an initial state
  237. * Returns:  nothing
  238. *****************************************************************************/
  239. void TBogBoard::ClearBoard ()
  240. {
  241.    for (int i = 0; i < 16; i++)
  242.       PBSquare[i]->reset();
  243.   
  244.    pbwip->resetString();
  245.    PBLastSquare->reSetSquare();
  246. }
  247.   
  248.  
  249. /*****************************************************************************
  250. * Function: FindAll
  251. * Parms:    none
  252. * Purpose:  Find the words that the user missed
  253. * Returns:  nothing
  254. *****************************************************************************/
  255. void TBogBoard::FindAll ()
  256. {
  257.  
  258.    char word[17];
  259.  
  260.    //This is used for debugging purposes!! TAKE IT OUT!!
  261.    //memcpy(fndall_str,"BOGGLEGAMEMASTER", 16);
  262.    for (int i = 0; i < 16; i++) 
  263.    {
  264.       word[0] = fndall_str[i];  //this function kicks of the process
  265.       word[1] = '\0';           // by calling the search function with
  266.       stack[0] = i;             // a starting point of the current letter
  267.       chflag[i] = TRUE;
  268.       Search(word, 1);
  269.       chflag[i] = FALSE;
  270.    }
  271. }
  272.  
  273.  
  274. /*****************************************************************************
  275. * Function: Search
  276. * Parms:    word, n
  277. * Purpose:  This recursive function will look up all the words you missed
  278. * Returns:  nothing
  279. *****************************************************************************/
  280. void TBogBoard::Search (char *word, int n)
  281. {
  282.    int r, k;
  283.    char  PBuf[50];
  284.   
  285.    r = brd_words->Lookup(word);
  286.    if (r > 0)
  287.    {
  288.       int irc = (int)pblist->locateText(word, false);
  289.   
  290.       /*****************************************************************
  291.       * Move the word over if it is more than two characters,  is
  292.       * not in the list,  and is found in the dictionary.
  293.       *****************************************************************/
  294.       if ((n > 2) && (strcmp(brd_words->getBuffer(), PBuf) != 0) && (irc < 0))
  295.       {
  296.          pblist->addString(word);
  297.          strcpy(PBuf, brd_words->getBuffer());
  298.       }
  299.    }
  300.    else if (r < 0) 
  301.         {
  302.            if ( (brd_words->prefix(word, brd_words->getBuffer())) == FALSE)
  303.               return;
  304.         }
  305.         else
  306.            return;
  307.  
  308.    /**************************************************
  309.    * This is the section that recursively looks up
  310.    * all possible words from a given starting point
  311.    **************************************************/
  312.    //Starting figures are as follows:
  313.    //  k=1, i=0, n=1        
  314.    for (int i = 0; (k = nums[stack[n-1]][i]) != -1; i++)
  315.    {
  316.       if (chflag[k] == FALSE) 
  317.       {
  318.          word[n] = fndall_str[k];   //add the next letter in the string to try
  319.          word[n+1] = '\0';          //BO the first time (debug mode)
  320.          stack[n] = k;
  321.          chflag[k] = TRUE;          //We were here already
  322.          Search(word, n+1);         //recursively call yourself
  323.          chflag[k] = FALSE;         //We are done with this recursive loop
  324.       }
  325.    }
  326. }
  327.